import {
  getForumById,getForumReplay,pushForumReplay
} from "../../api/getApi"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    forumDetail:{},
    replayData:[],
    myGame:[],
    inputText:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let myGame = wx.getStorageSync('myGame')
    if(!myGame){
      myGame = []
    }else{
      myGame = JSON.parse(myGame).slice(0, 20)
    }

    let forumDetail = wx.getStorageSync('forumDetail')
    if(!forumDetail){
      forumDetail = {}
    }else{
      forumDetail = JSON.parse(forumDetail)
    }
    if(forumDetail.content){
      forumDetail.content = forumDetail.content.replace(/\s+/g, "<br>");
    }
    this.setData({forumDetail:forumDetail,myGame:myGame})
    this.getData()
  },
  getData(){
    getForumReplay({page:1,limit:99,forumid:this.data.forumDetail.id}).then(res => {
      this.setData({replayData:res.list})
    })
  },

  uploadForum(){
    if(this.data.inputText.length<1){
      wx.showToast({
        title: "沉默是金，闷声发大财",
        icon: 'none'
      })
      return
    }
    if(this.checkFFGame()){
      return
    }
    let userData = JSON.parse(wx.getStorageSync('userData'))
    let pushData = {
      openid:wx.getStorageSync('openId'),
      forumid:this.data.forumDetail.id,
      head:userData.avatarUrl,
      name:userData.name,
      content:this.data.inputText
    }
    pushForumReplay(pushData).then(res=>{
      wx.showToast({
        title: res,
        icon: 'none'
      })
      this.getData()
      if(res=="禁止发表非法内容，如多次违反将永久封禁"){
        let ffCount = wx.getStorageSync('ffCount')?Number(wx.getStorageSync('ffCount')):0
        ffCount = ffCount + 1
        wx.setStorageSync('ffCount', ffCount)
      }
    })
  },
  checkFFGame(){
    let ffCount = wx.getStorageSync('ffCount')?Number(wx.getStorageSync('ffCount')):0
    if(ffCount>10){
      wx.showModal({
        title: '提示',
        content: '你因为多次恶意非法上传，已被禁止发布'
      })
      return true
    }
    if(ffCount>this.data.myGame.length){
      wx.showModal({
        title: '提示',
        content: '你因多次尝试上传非法内容，为防止恶意上传，你还需要再玩 ' +(ffCount - this.data.myGame.length)+' 款不同的游戏后才可继续上传发布'
      })
      return true
    }
    return false
  },
  onChange(e) {
    this.setData({
      inputText: e.detail.value,
    });
  },

  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})